package com.kayak.emailWorks.quartz.travelDays;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kayak.dbutil.dao.ComnDao;
import com.kayak.emailWorks.util.FetchEmail;
import com.kayak.emailWorks.util.EmailTempletMarket;
import com.kayak.kkutils.CalendarUtil;
import com.kayak.kkweb.sys.SysBeans;

public class GetTravelDaysResult {
	protected final static Logger log = LoggerFactory.getLogger(GetTravelDaysResult.class);

	public static void doIt() throws Exception {
		String username = EmailTempletMarket.FETCHEMAILADDRESS;
		String password = EmailTempletMarket.FETCHMAILPASSAGES;
		List<Map<String, Object>> newMsgList = null;
		try {
			newMsgList = new FetchEmail().getNewEmails(username, password);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("获取新邮件异常");
			return;
		}
		for (Map<String, Object> temp : newMsgList) {
			dealWithReturnMsg(temp, username, password);
		}
	}

	public static void dealWithReturnMsg(Map<String, Object> temp,String username, String password) throws Exception{
		ComnDao comnDao = SysBeans.getComnDao();
		String from = temp.get("from").toString();
		String personal = temp.get("personal").toString();
		String subject = temp.get("subject").toString();
		String content = temp.get("content").toString();
		Map<String, Object> map2 = null;
		map2 = new HashMap<String, Object>();
		if (subject.contains(EmailTempletMarket.SendMailTitle)) {
			EmailTempletMarket.DeleteAlreadyExist(temp);
		} else if (subject.contains(EmailTempletMarket.ContErrTile)
				|| subject.contains(EmailTempletMarket.SubErrTile)) {
			EmailTempletMarket.sendErrSubMail(username, password, personal, from);
			EmailTempletMarket.DeleteAlreadyExist(temp);
			return;
		} else {
			return;
		}
		Scanner sr1 = new Scanner(content);
		while (sr1.hasNext()) {
			String line1 = (String) sr1.nextLine();
			if(line1.contains("begin")) {
				if (line1.contains("/")) {
					EmailTempletMarket.DeleteAlreadyExist(temp);
					EmailTempletMarket.sendErrConMail(username, password, personal, from);
					return;
				}
				continue;
			}else if (line1.contains("end")) {
				if (line1.contains("/")) {
					EmailTempletMarket.DeleteAlreadyExist(temp);
					EmailTempletMarket.sendErrConMail(username, password, personal, from);
					return;
				}
				return;
			}else if (line1.contains("/")) {
				try {
					map2.put("mail_address", from);
					map2.put("travel_cities", line1.split("/")[0].toString().trim());
					map2.put("travel_days", line1.split("/")[1].toString().trim());
					map2.put("travel_time", line1.split("/")[2].toString().trim());
					map2.put("travle_descripetion", line1.toString().trim());
					map2.put("person", personal);
					map2.put("mail_date", CalendarUtil.getDate());
					map2.put("mail_time", CalendarUtil.getTime());
					map2.put("is_read", "1");
					comnDao.update("Einserttraveldaysinfo", map2);
				}catch (Exception e) {
					EmailTempletMarket.DeleteAlreadyExist(temp);
					EmailTempletMarket.sendErrConMail(username, password, personal, from);
					log.info("================="+personal+"的信息提取错误啦===============");
					e.printStackTrace();
					return;
				}
			}else if (line1.contains("\\")) {
					EmailTempletMarket.sendErrLinesMail(username, password, personal, from);
					EmailTempletMarket.DeleteAlreadyExist(temp);
					return;
			}else if (line1.contains("亲爱的")) {
					EmailTempletMarket.sendErrStartMail(username, password, personal, from);
					EmailTempletMarket.DeleteAlreadyExist(temp);
					return;
			}
		}
		return;
	}
}